package com.hsxxherp.ation.common.bean;

import com.google.common.base.Objects;
import com.hsxxherp.ation.common.error.AtionError;
import com.hsxxherp.ation.common.utils.json.AtionGsonBuilder;

/**
 * 内部服务响应
 */
public class Response {

    /**
     * 相应请求
     */
    private String code;
    private String msg;
    private Object data;
    // 追踪id
    private String rid;

    // 错误详情
    private String details;

    // 错误堆栈
    private String stacktrace;

    /**
     * 构建方法
     *
     * @return
     */
    public static ResponseBuilder builder() {
        return new ResponseBuilder();
    }

    /**
     * 快速构建成功响应
     * 返回code=“200” ，msg=“success”
     *
     * @param data
     * @return
     */
    public static ResponseBuilder ok(Object data) {
        return new ResponseBuilder().code("200").msg("success");
    }

    /**
     * 快速构建成功失败响应
     * 返回code=“200” ，msg=“success”,data="",rid=error.rid
     *
     * @param error
     * @return
     */
    public static ResponseBuilder error(AtionError error) {
        return new ResponseBuilder().code(error.getErrorCode()).msg(error.getErrorMessage()).data("").rid(error.getRid()).details(error.getDetails()).stacktrace(error.getStacktrace());
    }

    // getter
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    // setter
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getRid() {
        return rid;
    }

    public void setRid(String rid) {
        this.rid = rid;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getStacktrace() {
        return stacktrace;
    }

    public void setStacktrace(String stacktrace) {
        this.stacktrace = stacktrace;
    }

    /**
     * 将响应转为json格式
     *
     * @return
     */
    public String toString() {
        return AtionGsonBuilder.create().toJson(this);
    }

    @Override
    public boolean equals(Object object) {
        return Objects.equal(this, object);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(this);
    }

    public static class ResponseBuilder {

        private final Response response = new Response();

        private ResponseBuilder() {
        }

        public ResponseBuilder code(String code) {
            this.response.code = code;
            return this;
        }

        public ResponseBuilder msg(String msg) {
            this.response.msg = msg;
            return this;
        }

        public ResponseBuilder data(Object data) {
            this.response.data = data;
            return this;
        }

        public ResponseBuilder rid(String rid) {
            this.response.rid = rid;
            return this;
        }

        public ResponseBuilder details(String details) {
            this.response.details = details;
            return this;
        }

        public ResponseBuilder stacktrace(String stacktrace) {
            this.response.stacktrace = stacktrace;
            return this;
        }

        public Response build() {
            return response;
        }
    }
}
